<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
	<head>
		<jsp:include page="../inc.jsp"></jsp:include>
		<script type="text/javascript" src="../My97DatePicker/WdatePicker.js"></script>
		<script type="text/javascript" charset="utf-8">
		var lastIndex,id = '${id}';
		var IsInConfirm = '${IsInConfirm}';
		var IsOutConfirm = '${IsOutConfirm}';
		var tempid;
		var tempnum = 1;
		var tempbarcode = new Array();
		$(function() {
			var iuid = $('#deliverForm').find('input[name=iuid]');
			iuid.val('${dealerName}');

			var orname = $('#deliverForm').find('input[name=orname]');
			orname.val('${orname}');
			
			var num = $('#deliverForm').find('input[name=num]');
			num.val('${num}');
			
			var date = $('#deliverForm').find('input[name=date]');
			date.val('${date}');
			
			var serial = $('#deliverForm').find('input[name=serial]');
			serial.val('${serial}');
			
			var uid = $('#deliverForm').find('input[name=uid]');
			var uidCombobox = uid.combobox({
				url : '${pageContext.request.contextPath}/qxgl/userAction!noAuth_userInReperCombobox.html',
				valueField : 'id',
				textField : 'username',
				multiple : false,
				editable : false,
				panelHeight : 'auto'
			});
			
			$.ajax({
				url : '${pageContext.request.contextPath}/kcgl/goodsDeliverAction!noAuth_search.html',
				data : {id : id},
				type : "POST",
				success : function(d){
					var json = $.parseJSON(d);
					document.getElementById("items").value=json[0].itemsid;
					var f = $('#deliverForm');
					f.form("load",{
						id : json[0].id,
						date : json[0].date,
						serial : json[0].serial,
						uid : json[0].uid,
						irid : json[0].irid,
						orid : json[0].orid,
						num : json[0].num,
						remark :json[0].remark
					});
					$("#rid").val(json[0].orid);
					$.ajax({
						url : '${pageContext.request.contextPath}/kcgl/goodsDeliverAction!findGoodsDeliver.html?model.itemsid='+json[0].itemsid,
						type : "POST",
						success : function(d){
						var json = $.parseJSON(d);
						if(json.success!=false){
						var rows = $('#dg').datagrid('getRows');
						    var flag = 0;	
						    if(rows.length > 0){
						        for(var i = 0; i < rows.length; i++) {
						            var tempbarcode = $('#barcode').val();
						            if(tempbarcode == rows[i].barcode){	
						                tempnum = rows[i].num + 1;		   
						                $('#dg').datagrid('updateRow',{
		                                    index: i,
		                                    row: {
			                                      num: tempnum
		                                    }
	                                    });					                
						                flag = 1;
						                tempnum = 1;
						                return false;
						            }					            
							    }					        
						    }
					    if(flag == 0){
						var json = $.parseJSON(d);
						  for(var i=0;i<json.length;i++){
					      $('#dg').datagrid('appendRow',{
								itemsid:json[i].itemsid,
								barcode:json[i].barcode,
								id:json[i].id,
								type:json[i].type,
								gtname:json[i].gtname,
								bname:json[i].bname,
								name:json[i].name,
								unit:json[i].unit,
								color:json[i].color,
								size:json[i].size,
							    amount:json.amount,
								num: json[i].num
								})
								lastIndex = $('#dg').datagrid('getRows').length-1;
								$('#dg').datagrid('selectRow', lastIndex);
								 }					    
								return false;
								}
								}else{
									parent.sy.messagerShow({
												msg : json.msg,
												title : '提示'
									});
								}
								sumNum();
							}
						}
					)
					
				}
			});
			if(IsInConfirm=='false'&&IsOutConfirm=='false'){
			$('#dg').datagrid({
				//idField : 'id',
				toolbar:[{
					text: '确定',
					iconCls:'icon-edit',
					handler:function(){
						$('#dg').datagrid('acceptChanges');
						sumNum();
					}
				},'-',{
					text: '删行',
					iconCls:'icon-remove',
					handler:function(){
						var row = $('#dg').datagrid('getSelections');
						if (row){
						for ( var i = 0; i < row.length; i++) {
							if(row[i].itemsid!=null){
								$.ajax({
									url : '${pageContext.request.contextPath}/kcgl/goodsTransferItemAction!delete.html',
									data : {
										ids : row[i].itemsid
									},
									dataType : 'json',
									success : function(d) {
										$('#dg').datagrid('load');
										$('#dg').datagrid('unselectAll');
										parent.sy.messagerShow({
											title : '提示',
											msg : d.msg
										});
										sumNum();
									}
								});
							}else{
								var rows = $('#dg').datagrid('getSelected');
								var index = $('#dg').datagrid('getRowIndex', rows);
								$('#dg').datagrid('deleteRow', index);
							}
						}
						}
						
					}
				},'-',{
					text: '保存',
					iconCls:'icon-add',
					handler:function(){
						$('#dg').datagrid('acceptChanges');
						var items = [];
						var nums = [];
						var iids = [];
						var rows = $('#dg').datagrid('getRows');
						for ( var i = 0; i < rows.length; i++) {
							if(rows[i].itemsid!=null){
								iids.push(rows[i].itemsid);
							}else{
								iids.push('*');
							}
							items.push(rows[i].id);
							nums.push(rows[i].num);
						}
						$('#iids').val(iids.join(','));
						$('#nums').val(nums.join(','));
						$('#ids').val(items.join(','));
						$('#deliverForm').form('submit', {
							url : '${pageContext.request.contextPath}/kcgl/goodsTransferAction!edit.html',
							success : function(d) {
								var json = $.parseJSON(d);
								parent.sy.messagerShow({
									msg : json.msg,
									title : '提示'
								});
								if (json.success) {
									parent.showTab("编辑库存调拨单","库存调拨");
								}
							}
						});
					}
				}],
				onBeforeLoad:function(){
					$(this).datagrid('rejectChanges');
				},
				onClickRow:function(rowIndex){
					if (lastIndex != rowIndex){
						$('#dg').datagrid('endEdit', lastIndex);
						$('#dg').datagrid('beginEdit', rowIndex);
					}else{
						$('#dg').datagrid('endEdit', lastIndex);
						$('#dg').datagrid('beginEdit', rowIndex);
					}
					lastIndex = rowIndex;
					sumNum();
				}
			});
			}else{
			
				$('#dg').datagrid({
				toolbar:[{
					text: '库存调拨明细',
					iconCls:'icon-save',
				//	handler:function(){
				//}
			     	}]
				})
			}
			
			$("#barcode").live("keypress",function(e) {
				var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
				if (keyCode == 13) {
					_search();
					return false;
				}
				
			});
		});
	
		function _search() {
			$('#searchForm').form('submit', {
				url : '${pageContext.request.contextPath}/spgl/goodsAction!noAuth_search_deliver.html',
				success : function(d) { 
					var json = $.parseJSON(d);
					if(json.success!=false){
					var rows = $('#dg').datagrid('getRows');
					    var flag = 0;	
					    if(rows.length > 0){
					        for(var i = 0; i < rows.length; i++) {
					            var tempbarcode = $('#barcode').val();
					            if(tempbarcode == rows[i].barcode){	
					                tempnum = rows[i].num + 1;		   
					                $('#dg').datagrid('updateRow',{
	                                    index: i,
	                                    row: {
		                                      num: tempnum
	                                    }
                                    });					                
					                flag = 1;
					                tempnum = 1;
					                return false;
					            }					            
						    }					        
					    }
					    if(flag == 0){
						$('#dg').datagrid('endEdit', lastIndex);
						$('#dg').datagrid('appendRow',{
						  id:json.id,
							barcode:json.barcode,
							type:json.type,
							gtname:json.gtname,
							bname:json.bname,
							name:json.name,
							unit:json.unit,
							color:json.color,
							size:json.size,
							amount:json.amount,
							num: tempnum
						})
						lastIndex = $('#dg').datagrid('getRows').length-1;
						$('#dg').datagrid('selectRow', lastIndex);
						 }					    
							return false;
						}else{
							parent.sy.messagerShow({
									msg : json.msg,
									title : '提示'
						});
					}
					sumNum();
				}
			});
// 			cleanSearch();
		}
		
		function cleanSearch() {
			$('#searchForm input').val('');
		}
		
		function sumNum(){
			var rows = $('#dg').datagrid('getRows');
			var sum = 0;
			for ( var i = 0; i < rows.length; i++) {
				sum += Number(rows[i].num);
			}
			$('#num').val(sum);
		}
	</script>
	</head>
	<body class="easyui-layout" data-options="fit:true">
	<c:if test="${IsInConfirm==false&&IsOutConfirm==false}">
		<div data-options="region:'north',border:false" style="height: 142px; overflow: hidden;">
			<form id="deliverForm" style="height:110px;">
				<input name="id" type="hidden"/>
				<input id="items" name="items" type="hidden" />
				<input id="iids" name="iids" type="hidden" />
				
				<input id="nums" name="nums" type="hidden" />
				<input id="ids" name="ids" type="hidden" />
				<table class="tableForm datagrid-toolbar" style="width: 100%; height: 100%;">
					<tr>
						<th style="width: 80px;">发货日期</th>
						<td style="width: 200px;"><input name="date" id="datebox" class="Wdate" onclick="WdatePicker()" style="width: 200px;" data-options="required:'true',missingMessage:'请输入调拔日期'"/></td>
						<th style="width: 80px;">单&nbsp;&nbsp;&nbsp;&nbsp;号</th>
						<td style="width: 200px;"><input name="serial" style="width: 200px;" readonly="readonly" id="serial"/></td>
						<th style="width: 80px;">操&nbsp;作&nbsp;员</th>
						<td style="width: 200px;"><input name="uid" style="width: 200px;" data-options="required:'true',missingMessage:'请选择操作员'"/></td>
					</tr>
					<tr>
						<th>发货仓库</th>
						<td><input name="orname" readonly="readonly"  style="width: 200px;" data-options="required:'true',missingMessage:'请选择发货仓库'"/></td>
						<th>收货经销商</th>
						<td><input name="iuid" style="width: 200px;" data-options="required:'true',missingMessage:'请选择收货经销商'"/></td>
						<th>发货总数</th>
						<td><input name="num" id="num" style="width: 200px;" type="text" readonly="readonly"/></td>
					</tr>
					<tr>
						<th>备&nbsp;&nbsp;&nbsp;&nbsp;注</th>
						<td colspan="5"><textarea name="remark" style="height:25px;width: 95%;"></textarea></td>
					</tr>
				</table>
			</form>
			
			<form id="searchForm" style="height:10px;margin-top:0px;">
			<input type="hidden" id="rid" name="rid">
				<table class="tableForm datagrid-toolbar" style="width: 100%; height: 100%;">
					<tr>
						<th style="width:73px;">商品条码</th>
						<td style="width: 200px;"><input id="barcode" name="barcode" style="width: 200px;" /></td>
<!-- 						<th style="width: 80px;">款号/款式</th> -->
<!-- 						<td style="width: 200px;"><input name="type" style="width: 200px;" /></td> -->
						<td style="width: 280px;" align="right" colspan="4">
							<a href="javascript:void(0);" class="easyui-linkbutton" onclick="_search();">定位商品</a>
							<a href="javascript:void(0);" class="easyui-linkbutton" onclick="cleanSearch();">取消</a>
						</td>
					</tr>
				</table>
			</form>
			
		</div>
		</c:if>
	
		<c:if test="${IsInConfirm==true&&IsOutConfirm==false}">
		<div data-options="region:'north',border:false" style="height: 100px; overflow: hidden;">
			<form id="deliverForm" style="height:110px;">
				<input name="id" type="hidden"/>
				<input id="items" name="items" type="hidden" />
				<input id="iids" name="iids" type="hidden" />
				
				<input id="nums" name="nums" type="hidden" />
				<input id="ids" name="ids" type="hidden" />
				<table class="tableForm datagrid-toolbar" style="width: 100%; height: 100%;">
					<tr>
						<th style="width: 80px;">发货日期</th>
						<td style="width: 200px;"><input name="date" id="datebox" class="Wdate" onclick="WdatePicker()" style="width: 200px;" data-options="required:'true',missingMessage:'请输入调拔日期'"/></td>
						<th style="width: 80px;">单&nbsp;&nbsp;&nbsp;&nbsp;号</th>
						<td style="width: 200px;"><input name="serial" style="width: 200px;" readonly="readonly" id="serial"/></td>
						<th style="width: 80px;">操&nbsp;作&nbsp;员</th>
						<td style="width: 200px;"><input name="uid" style="width: 200px;" data-options="required:'true',missingMessage:'请选择操作员'"/></td>
					</tr>
					<tr>
						<th>发货仓库</th>
						<td><input name="orname" readonly="readonly"  style="width: 200px;" data-options="required:'true',missingMessage:'请选择发货仓库'"/></td>
						<th>收货经销商 </th>
						<td><input name="iuid" style="width: 200px;" data-options="required:'true',missingMessage:'请选择收货经销商'"/></td>
						<th>发货总数</th>
						<td><input name="num" id="num" style="width: 200px;" type="text" readonly="readonly"/></td>
					</tr>
					<tr>
						<th>备&nbsp;&nbsp;&nbsp;&nbsp;注</th>
						<td colspan="5"><textarea name="remark" style="height:30px;width: 95%;"></textarea></td>
					</tr>
				</table>
			</form>
			</div>
		</c:if>
		<c:if test="${IsInConfirm==false&&IsOutConfirm==true}">
		<div data-options="region:'north',border:false" style="height: 100px; overflow: hidden;">
			<form id="deliverForm" style="height:110px;">
				<input name="id" type="hidden"/>
				<input id="items" name="items" type="hidden" />
				<input id="iids" name="iids" type="hidden" />
				
				<input id="nums" name="nums" type="hidden" />
				<input id="ids" name="ids" type="hidden" />
				<table class="tableForm datagrid-toolbar" style="width: 100%; height: 100%;">
					<tr>
						<th style="width: 80px;">发货日期</th>
						<td style="width: 200px;"><input name="date" id="datebox" class="Wdate" onclick="WdatePicker()" style="width: 200px;" data-options="required:'true',missingMessage:'请输入调拔日期'"/></td>
						<th style="width: 80px;">单&nbsp;&nbsp;&nbsp;&nbsp;号</th>
						<td style="width: 200px;"><input name="serial" style="width: 200px;" readonly="readonly" id="serial"/></td>
						<th style="width: 80px;">操&nbsp;作&nbsp;员</th>
						<td style="width: 200px;"><input name="uid" style="width: 200px;" data-options="required:'true',missingMessage:'请选择操作员'"/></td>
					</tr>
					<tr>
						<th>发货仓库</th>
						<td><input name="orname" readonly="readonly"  style="width: 200px;" data-options="required:'true',missingMessage:'请选择发货仓库'"/></td>
						<th>收货经销商</th>
						<td><input name="iuid" style="width: 200px;" data-options="required:'true',missingMessage:'请选择收货经销商'"/></td>
						<th>发货总数</th>
						<td><input name="num" id="num" style="width: 200px;" type="text" readonly="readonly"/></td>
					</tr>
					<tr>
						<th>备&nbsp;&nbsp;&nbsp;&nbsp;注</th>
						<td colspan="5"><textarea name="remark" style="height:30px;width: 95%;"></textarea></td>
					</tr>
				</table>
			</form>
			</div>
		</c:if>
		<c:if test="${IsInConfirm==true&&IsOutConfirm==true}">
		<div data-options="region:'north',border:false" style="height: 100px; overflow: hidden;">
			<form id="deliverForm" style="height:110px;">
				<input name="id" type="hidden"/>
				<input id="items" name="items" type="hidden" />
				<input id="iids" name="iids" type="hidden" />
				
				<input id="nums" name="nums" type="hidden" />
				<input id="ids" name="ids" type="hidden" />
				<table class="tableForm datagrid-toolbar" style="width: 100%; height: 100%;">
					<tr>
						<th style="width: 80px;">发货日期</th>
						<td style="width: 200px;"><input name="date" id="datebox" class="Wdate" onclick="WdatePicker()" style="width: 200px;" data-options="required:'true',missingMessage:'请输入调拔日期'"/></td>
						<th style="width: 80px;">单&nbsp;&nbsp;&nbsp;&nbsp;号</th>
						<td style="width: 200px;"><input name="serial" style="width: 200px;" readonly="readonly" id="serial"/></td>
						<th style="width: 80px;">操&nbsp;作&nbsp;员</th>
						<td style="width: 200px;"><input name="uid" style="width: 200px;" data-options="required:'true',missingMessage:'请选择操作员'"/></td>
					</tr>
					<tr>
						<th>发货仓库</th>
						<td><input name="orname" readonly="readonly"  style="width: 200px;" data-options="required:'true',missingMessage:'请选择发货仓库'"/></td>
						<th>收货经销商 </th>
						<td><input name="iuid" style="width: 200px;" data-options="required:'true',missingMessage:'请选择收货经销商'"/></td>
						<th>发货总数</th>
						<td><input name="num" id="num" style="width: 200px;" type="text" readonly="readonly"/></td>
					</tr>
					<tr>
						<th>备&nbsp;&nbsp;&nbsp;&nbsp;注</th>
						<td colspan="5"><textarea name="remark" style="height:30px;width: 95%;"></textarea></td>
					</tr>
				</table>
			</form>
			</div>
		</c:if>
		<div data-options="region:'center',border:false" style="overflow: hidden;">
			<table id="dg" class="easyui-datagrid" style="height:auto"  data-options="iconCls: 'icon-edit',singleSelect: true">  
		        <thead>
		            <tr>
		         	   <th data-options="field:'itemsid',width:120,hidden:true">调拨明细ID</th>
		            	<th data-options="field:'id',width:80,hidden:true">商品ID</th>
		            	<th data-options="field:'barcode',width:130">商品条码</th>
		            	<th data-options="field:'name',width:100">商品名称</th>
		            	<th data-options="field:'bname',width:100">品牌</th>
		            	<th data-options="field:'gtname',width:80">商品类别</th>  
		                <th data-options="field:'type',width:100">款号/款式</th>  
                   		<th data-options="field:'color',width:60">颜色</th>
                   		<th data-options="field:'size',width:60">尺码</th>
                   		<th data-options="field:'unit',width:60">单位</th>
		                <th data-options="field:'num',width:100,align:'center',editor:{type:'numberbox'}">发货总数</th>
		            </tr>
		        </thead>
		    </table>
		</div>
	</body>
</html>